package com.zdoon.controller;

import com.zdoon.pojo.Dept;
import com.zdoon.pojo.Result;
import com.zdoon.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

//    获取部门列表
    @GetMapping
    public Result list() {
        System.out.println("hello world");
        log.info("获取部门列表");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

//    根据id删除部门
    @DeleteMapping
    public Result delete(Integer id) {
        log.info("删除部门id:{}", id);
        deptService.deleteById(id);
        return Result.success();
    }

//    添加部门
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        log.info("添加部门:{}", dept);
        deptService.add(dept);
        return Result.success();
    }

//    根据id查询部门
    @GetMapping("{id}")
    public Result get(@PathVariable Integer id) {
        log.info("查询部门id:{}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

//    更新部门
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("更新部门:{}", dept);
        deptService.update(dept);
        return Result.success();
    }
}
